/**
 * @description user controller
 * @author 追追
 */

const {
    getUserInfo,
    createUser,
    deleteUser,
    updateUser,
} = require('../services/user');
const { SuccessModel, ErrorModel } = require('../model/ResModel');
const {
    registerUserNameNotExistInfo,
    registerUserNameExistInfo,
    registerFailInfo,
    loginFailInfo,
    deleteUserFailInfo,
    changeInfoFailInfo,
    changePasswordFailInfo
} = require('../model/ErrorInfo');
const doCrypto = require('../utils/cryp');
/**
 * 用户名是否存在
 * @param {string} userName
 */
async function isExist(userName) {
    const userInfo = await getUserInfo(userName);
    if (userInfo) {
        //存在
        return new SuccessModel(userInfo);
    } else {
        //不存在
        return new ErrorModel(registerUserNameNotExistInfo);
    }
}

/**
 *
 * @param {string} userName
 * @param {string} password
 * @param {number} gender  性别 1男 2女 3保密
 */
async function register({ userName, password, gender }) {
    const userInfo = await getUserInfo(userName);
    if (userInfo) {
        return new ErrorModel(registerUserNameExistInfo);
    }
    try {
        await createUser({
            userName,
            password: doCrypto(password),
            gender,
        });
        return new SuccessModel();
    } catch (e) {
        console.error(e.message, e.stack);
        return new ErrorModel(registerFailInfo);
    }
}

/**
 * 登录
 * @param {Object} ctx koa2 ctx
 * @param {String} userName 用户名
 * @param {String} password 密码
 */
async function login(ctx, userName, password) {
    //登录成功 ctx.session.userInfo = xxx
    const userInfo = await getUserInfo(userName, doCrypto(password));
    //登录失败
    if (!userInfo) {
        return new ErrorModel(loginFailInfo);
    }

    //登录成功
    if (ctx.session.userInfo == null) {
        ctx.session.userInfo = userInfo;
    }
    return new SuccessModel();
}

/**
 * 删除当前用户
 * @param {string} userName
 */
async function deleteCurUser(userName) {
    const result = deleteUser(userName);
    if (result) {
        return new SuccessModel();
    }
    return new ErrorModel(deleteUserFailInfo);
}

//修改用户信息
async function changeInfo(ctx, { nickName, city, picture }) {
    const { userName } = ctx.session.userInfo;
    if (!nickName) {
        nickName = userName;
    }
    const reasult = await updateUser(
        { newNickName: nickName, newCity: city, newPicture: picture },
        { userName }
    );
    if (reasult) {
        ctx.session.userInfo = Object.assign(ctx.session.userInfo, {
            nickName,
            city,
            picture,
        });
        return new SuccessModel();
    }
    return new ErrorModel(changeInfoFailInfo);
}

//修改密码
async function changePassword(userName, password, newPassword) {
    const result = await updateUser(
        {
            newPassword: doCrypto(newPassword),
        },
        {
            userName,
            password: doCrypto(password),
        }
    );
    if(result){
        return new SuccessModel();
    }
    return new ErrorModel(changePasswordFailInfo);
}
async function logout(ctx){
    delete ctx.session.userInfo;
    return new SuccessModel();
}

module.exports = {
    isExist,
    register,
    login,
    deleteCurUser,
    changeInfo,
    changePassword,
    logout
};
